
-- Player meta functions

pmeta = FindMetaTable( "Player" )

function pmeta:GetOrg()
	for k, v in pairs( organization.GetAll() ) do
		for k2, v2 in pairs( v.members ) do
			if v2 == self then
				return v
			end
		end
	end
	return false
end

function pmeta:GetOrgLeader()
	for k, v in pairs( organization.GetAll() ) do
		for k2, v2 in pairs( v.members ) do
			if v2 == self then
				return v.leader
			end
		end
	end
	return self
end

function pmeta:IsInOrg( org )
	if org == nil then 
		if self:GetOrg() == false then return false end 
		return true
	end
	for k, v in pairs( org:GetMembers() ) do
		if v == self then
			return true
		end
	end
	return false
end

function pmeta:HasMember( ply )
	if self:GetOrg() == false then return false end
	if ply:GetOrg() == false then return false end
	return ply:IsInOrg( self:GetOrg() ) 
end

function playerSetOrgSpawn( ply )

	if !ply:IsInOrg() then return end
	if !ply:GetOrg():GetSpawn() then return end
	
	ply:SetPos( ply:GetOrg():GetSpawn() )
	
end
hook.Add( "PlayerSpawn", "playerSetOrgSpawn", playerSetOrgSpawn )
